cmake_minimum_required (VERSION 2.6)
# We are only interested in finding jni.h: we do not care about extended JVM
# functionality or the AWT library.
set(JAVA_AWT_LIBRARY NotNeeded)
set(JAVA_JVM_LIBRARY NotNeeded)
if(CMAKE_SYSTEM_NAME STREQUAL "Android")
	# android 平台没有jni_md.h
	set(JAVA_INCLUDE_PATH2 NotNeeded)
endif()

set(JAVA_AWT_INCLUDE_PATH NotNeeded)
find_package(JNI REQUIRED)
message(STATUS JAVA_INCLUDE_PATH=${JAVA_INCLUDE_PATH})

# 加入jni支持
include_directories(${JAVA_INCLUDE_PATH})
if(NOT CMAKE_SYSTEM_NAME STREQUAL "Android")
	include_directories(${JAVA_INCLUDE_PATH2})
endif()
# 查找OpenMP
find_package(OpenMP)
# CPU体系对应的SIMD指令集名称
set(    arm_SIMD neon CACHE INTERNAL "SIMD intruction set for arm architecture")
set(   mips_SIMD msa  CACHE INTERNAL "SIMD intruction set for mips architecture")
set(    x86_SIMD avx2 CACHE INTERNAL "SIMD intruction set for x86 architecture")
set( x86_64_SIMD avx2 CACHE INTERNAL "SIMD intruction set for x86_64 architecture")
set(ARCH_TYPE ${CMAKE_SYSTEM_PROCESSOR})
if(CMAKE_GENERATOR_PLATFORM)
	set(ARCH_TYPE ${CMAKE_GENERATOR_PLATFORM})
endif()
if( ARCH_TYPE MATCHES "x86_64|x64|AMD64|Win64")
	set(ARCH_TYPE  "x86_64")
elseif(ARCH_TYPE MATCHES  "Win32|x86")
	set(ARCH_TYPE  "x86")
endif()
set(ARCH_SIMD ${${ARCH_TYPE}_SIMD})


# 当前文件夹下所有c/cpp文件
aux_source_directory(${CMAKE_CURRENT_LIST_DIR} _SRC_SOURCES)
#设置变量，表示所有的源文件列表  
set(SOURCE_FILES )
list(APPEND SOURCE_FILES ${DEPENDENT_SOURCE_DIR}/jni_utilits.cpp ${_SRC_SOURCES})

add_library(${JNI_JIU_LIBNAME} SHARED ${SOURCE_FILES})
# 使用静态库
use_static_library(${JNI_JIU_LIBNAME})
# android交叉编译添加log库用于调试
#target_link_libraries(${JNI_JIU_LIBNAME} $<$<PLATFORM_ID:Android>:log>)


target_include_directories (${JNI_JIU_LIBNAME}	PRIVATE ${DEPENDENT_SOURCE_DIR})
#############设置动态库后缀###################
set_target_properties(${JNI_JIU_LIBNAME} PROPERTIES DEBUG_POSTFIX _d)
# 设置SIMD指令优化选项
# 开启OpenMP
target_compile_options(${JNI_JIU_LIBNAME} PRIVATE 
	$<$<AND:$<CXX_COMPILER_ID:MSVC>,$<CONFIG:Release>>:-arch:$<UPPER_CASE:${ARCH_SIMD}>> 
	$<$<AND:$<CXX_COMPILER_ID:GNU>,$<CONFIG:Release>,$<NOT:$<STREQUAL:$<LOWER_CASE:${ARCH_TYPE}>,arm>>>:-fopt-info -m$<LOWER_CASE:${ARCH_SIMD}>> 		
	$<$<AND:$<CXX_COMPILER_ID:GNU>,$<CONFIG:Release>,$<STREQUAL:$<LOWER_CASE:${ARCH_TYPE}>,arm>>: ftree-vectorize -mfpu=$<UPPER_CASE:${ARCH_SIMD}>>
	$<$<BOOL:${OpenMP_FOUND}>:${OpenMP_CXX_FLAGS}>		
	)
#######Windows 下DLL 文件名前缀为空 #######
if(CMAKE_SYSTEM_NAME MATCHES "Windows")
	set_property(TARGET ${JNI_JIU_LIBNAME} PROPERTY  PREFIX "")
endif(CMAKE_SYSTEM_NAME MATCHES "Windows")
########安装脚本##########
install(TARGETS ${JNI_JIU_LIBNAME} 
        RUNTIME DESTINATION ${RUNTIME_INSTALL_DIR}
        LIBRARY DESTINATION ${LIBRARY_INSTALL_DIR}
        )
if(MSVC)
	# 安装pdb文件
	install(FILES $<TARGET_PDB_FILE:${JNI_JIU_LIBNAME}> DESTINATION bin OPTIONAL)
endif(MSVC)
